package com.ikun.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.ikun.entity.Attendances;
import com.ikun.service.AttendancesService;
import com.ikun.util.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 考勤表(Attendances)表控制层
 *
 * @author Li
 * @since 2025-03-26 10:52:33
 */
@RestController
@RequestMapping("/attendances")
@Tag(name = "考勤管理", description = "考勤相关操作接口")
public class AttendancesController {

    @Autowired
    private AttendancesService attendancesService;

    @GetMapping("/all")
    @ResponseBody
    @Operation(summary = "获取所有考勤记录", description = "返回所有考勤记录")
    public Result allAttendances(@RequestParam(required = false) String recordDate,
        @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
        return Result.success(attendancesService.allAttendances(recordDate, startTime, endTime));
    }

    @GetMapping("/getCurrentTime")
    @Operation(summary = "获取当前时间", description = "返回精确到秒的当前年月日时间")
    public String getCurrentTime() {
        return LocalDateTime.now().toString();
    }

    @PostMapping("/clock_In")
    @Operation(summary = "上班打卡", description = "根据当前时间判断打卡状态，超过 12:00 无法打卡")
    public Result clock_in(Integer request) {
        LocalTime currentTime = LocalTime.now();
        return attendancesService.clock_in(request, currentTime);
    }

    @PostMapping("/clock_Out")
    @Operation(summary = "下班打卡", description = "根据当前时间判断下班打卡状态")
    public Result clock_out(Integer request) {
        LocalTime currentTime = LocalTime.now();
        return attendancesService.clock_out(request, currentTime);

    }

    @PostMapping("/leave")
    @Operation(summary = "提交请假申请", description = "员工提交请假申请")
    public Result insertStatus(Integer request, LocalDate recordDate) {
        return attendancesService.insertStatus(request, recordDate, 4);
    }

    @GetMapping("/records")
    @Operation(summary = "查询指定时间的考勤记录", description = "根据员工ID和日期范围查询考勤记录")
    public Result<List<Attendances>> getAttendanceRecords(
        @Parameter(description = "员工ID") @RequestParam("staff_id") Integer staffId,
        @Parameter(description = "开始日期，格式：YYYY-MM-DD") @RequestParam("start_date") LocalDate startDate,
        @Parameter(description = "结束日期，格式：YYYY-MM-DD") @RequestParam("end_date") LocalDate endDate) {
        // 构建查询参数
        Map<String, Object> params = Map.of("staff_id", staffId, "start_date", startDate, "end_date", endDate);
        // 调用Service层方法进行查询
        return attendancesService.getAttendanceRecords(params);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "获取考勤记录", description = "根据 ID 获取考勤记录")
    @ResponseBody
    public ResponseEntity<Attendances> queryById(@PathVariable("id") Integer id) {
        System.out.println("id = " + id);
        return ResponseEntity.ok(this.attendancesService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param attendances 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Attendances> add(Attendances attendances) {
        return ResponseEntity.ok(this.attendancesService.insert(attendances));
    }

    /**
     * 编辑数据
     *
     * @param attendances 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Attendances> edit(Attendances attendances) {
        return ResponseEntity.ok(this.attendancesService.update(attendances));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.attendancesService.deleteById(id));
    }

    @PostMapping("/allAttendances")
    @Operation(summary = "获取id下所有考勤记录", description = "返回所有考勤记录")
    public Result getAllAttendanceByStuffId(Integer stuffId) {
        System.out.println("stuffId = " + stuffId);
        return Result.success(attendancesService.getAllAttendance(stuffId));
    }

}
